//
//  LiveCellNode.m
//  EducationLive
//
//  Created by Apple on 2017/9/30.
//  Copyright © 2017年 ZhouHao. All rights reserved.
//

#import "LiveCellNode.h"

@interface LiveCellNode ()
// UI
@property (nonatomic, strong) ASNetworkImageNode *imageNode;
@property (nonatomic, weak) ASImageNode *lineImageNode;
@property (nonatomic, weak) ASTextNode *titleNode;
// Data
@property (nonatomic, strong) CateModel *item;

@end

@implementation LiveCellNode

- (instancetype)initWithItem:(CateModel *)item
{
    if (self = [super init]) {
        _item = item;
        // 添加图片
        [self addImageNode];
        // 添加标题
        [self addTitleNode];
        // 加载线
        [self addLineImageNode];
    }
    return self;
}
// 添加图片
- (void)addImageNode
{
    ASNetworkImageNode *imageNode = [[ASNetworkImageNode alloc]init];
    imageNode.placeholderEnabled = YES;
    imageNode.defaultImage = [UIImage imageNamed:@"http://192.168.1.121/Uploads/advert/1.png"];
    imageNode.URL = [NSURL URLWithString:_item.img];
    imageNode.layerBacked = YES;
    [self addSubnode:imageNode];
    _imageNode = imageNode;
}
// 添加标题
- (void)addTitleNode
{
    ASTextNode *titleNode = [[ASTextNode alloc]init];
    titleNode.placeholderEnabled = YES;
    titleNode.placeholderColor = RGB_255(245, 245, 245);
    titleNode.layerBacked = YES;
    titleNode.maximumNumberOfLines = 1;
    NSDictionary *attrs = @{ NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:12.0f] ,NSForegroundColorAttributeName: RGB_255(36, 36, 36)};
    titleNode.attributedText = [[NSAttributedString alloc]initWithString:_item.name ? _item.name :@"" attributes:attrs];
    [self addSubnode:titleNode];
    _titleNode = titleNode;
}

// 加载线
- (void)addLineImageNode
{
    ASImageNode *lineImageNode = [[ASImageNode alloc]init];
    lineImageNode.layerBacked = YES;
    lineImageNode.backgroundColor = [UIColor clearColor];
    [self addSubnode:lineImageNode];
    _lineImageNode = lineImageNode;
}

- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
{
    _imageNode.style.preferredSize = CGSizeMake(29, 29);
    _lineImageNode.style.preferredSize = CGSizeMake(48, 0.5);
    _titleNode.style.flexShrink = YES;
    ASStackLayoutSpec *titleLayout = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical spacing:6 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsCenter children:@[_imageNode,_titleNode]];
    ASStackLayoutSpec *lineLayout = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical spacing:0 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsCenter children:@[titleLayout,_lineImageNode]];
    return lineLayout;
}

@end


@interface LiveAdCellNode ()

// UI
@property (nonatomic, weak) ASNetworkImageNode *adImageNode;
// Data
@property (nonatomic, strong) AdvertModel *advertModel;

@end

@implementation LiveAdCellNode

- (instancetype)initWithItem:(AdvertModel *)advertModel
{
    if (self = [super init]) {
        
        _advertModel = advertModel;
        // 设置背景颜色
        self.backgroundColor = RGB_255(245,245,245);
        // 加载广告图片
        [self addAdImageNode];
    }
    return self;
}
// 加载广告图片
- (void)addAdImageNode
{
    ASNetworkImageNode *adImageNode = [[ASNetworkImageNode alloc]init];
    adImageNode.layerBacked = YES;
    adImageNode.defaultImage = [UIImage imageNamed:@""];
    adImageNode.URL = [NSURL URLWithString:_advertModel.img];
    [self addSubnode:adImageNode];
    _adImageNode = adImageNode;
}

- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
{
    _adImageNode.style.preferredSize = CGSizeMake(kScreenWidth, 60);
    ASInsetLayoutSpec *insetLayout = [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsMake(10, 0, 10, 0) child:_adImageNode];
    
    return insetLayout;
}

@end


@interface LiveRecommendCellNode ()
// UI
@property (nonatomic, weak) ASImageNode *imageNode;
//@property (nonatomic, weak) ASNetworkImageNode *adImageNode;
@property (nonatomic, weak) ASTextNode *titleNode;
@property (nonatomic, weak) ASTextNode *priceNode;
// Data

@end

@implementation LiveRecommendCellNode

- (instancetype)initWithItem
{
    if (self = [super init]) {
        // 添加图片
        [self addImageNode];
        // 添加标题
        [self addTitleNode];
        // 添加价格
        [self addPriceNode];
    }
    return self;
}
// 添加图片
- (void)addImageNode
{
    ASImageNode *imageNode = [[ASImageNode alloc]init];
    imageNode.placeholderEnabled = YES;
    imageNode.placeholderColor = RGB_255(245, 245, 245);
    imageNode.image = [UIImage imageNamed:@"home_hot_1"];
    imageNode.layerBacked = YES;
    [self addSubnode:imageNode];
    _imageNode = imageNode;
}
// 添加标题
- (void)addTitleNode
{
    ASTextNode *titleNode = [[ASTextNode alloc]init];
    titleNode.layerBacked = YES;
    titleNode.maximumNumberOfLines = 1;
    NSDictionary *attrs = @{ NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:13.0f] ,NSForegroundColorAttributeName: RGB_255(36, 36, 36)};
    titleNode.attributedText = [[NSAttributedString alloc]initWithString:@"测试标题" attributes:attrs];
    [self addSubnode:titleNode];
    _titleNode = titleNode;
}
// 添加价格
- (void)addPriceNode
{
    ASTextNode *priceNode = [[ASTextNode alloc]init];
    priceNode.layerBacked = YES;
    priceNode.maximumNumberOfLines = 1;
    NSDictionary *attrs = @{ NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:12.0f] ,NSForegroundColorAttributeName: GreenColor};
    priceNode.attributedText = [[NSAttributedString alloc]initWithString:@"免费" attributes:attrs];
    [self addSubnode:priceNode];
    _priceNode = priceNode;
}

- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
{
    _imageNode.style.preferredSize = CGSizeMake((kScreenWidth - 20 - 12)/2, (95 * (kScreenWidth - 20 - 12)/2) /170);
    ASStackLayoutSpec *titleStackLayout = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical spacing:3 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsStart children:@[_titleNode,_priceNode]];
    
    ASStackLayoutSpec *verStackLayout = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical spacing:5 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsStart children:@[_imageNode,titleStackLayout]];
    return verStackLayout;
}

@end
